  <template>
      <div class="edit-popup">
          <popup ref="popupRef" :title="popupTitle" :async="true" width="75%" :clickModalClose="true"
              @confirm="handleSubmit" @close="handleClose">
              <el-form ref="formRef" :model="formData" label-width="200px" :rules="formRules" :inline="true"
                  class="porjectFormRef">
                  <el-form-item label="项目名称" prop="projectName">
                      <el-input v-model="formData.projectName" placeholder="请输入项目名称" />
                  </el-form-item>
                  <el-form-item label="申报条件" prop="declarationConditions">
                      <el-input v-model="formData.declarationConditions" placeholder="请输入申报条件" type="textarea"
                          :autosize="{ minRows: 4, maxRows: 6 }" />
                  </el-form-item>
                  <el-form-item label="项目关键要素" prop="projectKey">
                      <el-input v-model="formData.projectKey" placeholder="请输入项目关键要素" type="textarea"
                          :autosize="{ minRows: 4, maxRows: 6 }" />
                  </el-form-item>
                  <el-form-item label="核心要素" prop="keyElement">
                      <el-input v-model="formData.keyElement" placeholder="请输入核心要素" type="textarea"
                          :autosize="{ minRows: 4, maxRows: 6 }" />
                  </el-form-item>
              </el-form>
          </popup>
      </div>
  </template>
  <script lang="ts" setup>
  import type { FormInstance } from 'element-plus'
  import { detailEdit, detailAdd, detailDetail } from '@/api/detail'
  import Popup from '@/components/popup/index.vue'
  import feedback from '@/utils/feedback'
  import type { PropType } from 'vue'
  defineProps({
      dictData: {
          type: Object as PropType<Record<string, any[]>>,
          default: () => ({})
      }
  })
  const emit = defineEmits(['success', 'close'])
  const formRef = shallowRef<FormInstance>()
  const popupRef = shallowRef<InstanceType<typeof Popup>>()
  const mode = ref('add')
  const popupTitle = computed(() => {
      return mode.value == 'edit' ? '编辑项目基本信息' : '新增项目基本信息'
  })

  const formData = reactive({
      id: '',
      projectName: '',
      declarationConditions: '',
      projectKey: '',
      keyElement: ''
  })

  const formRules = {}

  const handleSubmit = async () => {
      await formRef.value?.validate()
      const data: any = { ...formData }
      mode.value == 'edit' ? await detailEdit(data) : await detailAdd(data)
      popupRef.value?.close()
      feedback.msgSuccess('操作成功')
      emit('success')
  }

  const open = (type = 'add') => {
      mode.value = type
      popupRef.value?.open()
  }

  const setFormData = async (data: Record<string, any>) => {
      for (const key in formData) {
          if (data[key] != null && data[key] != undefined) {
              //@ts-ignore
              formData[key] = data[key]
          }
      }
  }

  const getDetail = async (row: Record<string, any>) => {
      const data = await detailDetail({
          id: row.id
      })
      setFormData(data)
  }

  const handleClose = () => {
      emit('close')
  }

  defineExpose({
      open,
      setFormData,
      getDetail
  })
  </script>

  <style scoped>
  .porjectFormRef .el-form-item {
      width: 500px;
  }

  ::v-deep .el-collapse-item__header {
    padding-left: 54px;
    background-color: var(--el-bg-color-collapse);
    font-size: 16px;
    font-weight: 600;
    color: var(--el-text-color-collapse);
  }

  ::v-deep .el-collapse-item__wrap {
      padding-top: 40px;
  }
  </style>
